package com.qf.mybatisplus.function.calculator;

/**
 * @author 12136
 * @version 1.0
 * @description: TODO 房贷计算器
 * @date 2022/4/21 9:17
 */
public class MortgageCalculator {


    private static double loanMoney;  //贷款金额

    private static int years; //贷款年限

    private static double yIns; //年利率

    private static double totalMoney;  //还款金额（本金+利息）

    private static double totalInterests; //利息总额

    private static double detail[][];  //每月还款详情


    //等额本息
    public static  void EqualPrincipalAndInterestMethod(double loanMoney,int years,double yIns) {

        double mIns = yIns / 100 / 12; //月利率
        int months = (years * 12); //还款所需月份
        double pow = Math.pow(1 + mIns,months);
        double remains = loanMoney;
        totalMoney = (months * loanMoney * mIns * pow) / (pow - 1);  //总还款金额
        totalMoney = Math.floor(totalMoney * 100 + 0.5) / 100;  //floor函数 保留两位小数
        totalInterests = totalMoney - loanMoney;
        totalInterests = Math.floor(totalInterests * 100 + 0.5) / 100;
        double temp[][] = new double[months][3];
        for (int i = 0; i < months; i++) {
            if(i == months - 1) {
                temp[i][1] = remains * mIns;
                temp[i][1] = Math.floor(temp[i][1] * 100 + 0.5) / 100;
                temp[i][0] = remains;
                temp[i][0] = Math.floor(temp[i][0] * 100 + 0.5) / 100;
                temp[i][2] = temp[i][0] + temp[i][1];
                temp[i][2] = Math.floor(temp[i][2] * 100 + 0.5) / 100;
                break;
            }
            //由于精度问题 最后一个月实际的本金会有差别 需要单独计算
            temp[i][1] = remains * mIns;
            temp[i][1] = Math.floor(temp[i][1] * 100 + 0.5) / 100;
            temp[i][2] = totalMoney / months;
            temp[i][2] = Math.floor(temp[i][2] * 100 + 0.5) / 100;
            temp[i][0] = temp[i][2] - temp[i][1];
            temp[i][0] = Math.floor(temp[i][0] * 100 + 0.5) / 100;
            remains -= temp[i][0];
        }
        //temp[][0]为每月还款本金 temp[][1]为每月还款利息 temp[][2]为每月还款总额
        detail = temp;
    }
    //等额本金
    public void EqualPrincipalMethod() {

        double mIns = yIns / 100 / 12; //月利率
        int months = (years * 12);
        double remains = loanMoney;
        double sum = 0; // 总计还款金额
        double temp[][] = new double[months][3];
        for (int i = 0; i < months; i++)
        {
            temp[i][0] = loanMoney / months;
            temp[i][0] = Math.floor(temp[i][0] * 100 + 0.5) / 100;
            temp[i][1] = remains * mIns;
            temp[i][1] = Math.floor(temp[i][1] * 100 + 0.5) / 100;
            remains -= temp[i][0];
            temp[i][2] = temp[i][0] + temp[i][1];
            temp[i][2] = Math.floor(temp[i][2] * 100 + 0.5) / 100;
            sum += temp[i][2];
        }
        //temp[][0]为每月还款本金 temp[][1]为每月还款利息 temp[][2]为每月还款总额
        detail = temp;
        totalMoney = sum;
        totalMoney = Math.floor(totalMoney * 100 + 0.5) / 100;
        totalInterests = totalMoney - loanMoney;
        totalInterests = Math.floor(totalInterests * 100 + 0.5) / 100;
    }

    public static void main(String[] args) {
//        MortgageCalculator.EqualPrincipalAndInterestMethod(80,30,2.75);
//      for (int i = 0;i<30;i++){
//          System.err.println(i+1+":"+detail[i][0]*10000+","+detail[i][1]*10000+","+detail[i][2]*10000);
//      }

        System.err.println(Math.floor(101 * 100 + 0.5) / 100);
    }

}
